/**
 * @param {character[][]} board
 * @param {string} word
 * @return {boolean}
 */
var exist = function (board, word) {
  let w = [];
  let i = 0;
  let flag = false;
  // 保存每个字符的上下左右位置坐标，长度同word长度
  let positionMap = [];
  while(i <= word.length - 1) {
    const code = word.charAt(i);
    const round = findRound(board, code);
    // 没有找到字符
    if(round.length === 0) {
      flag = true;
      break;
    }
    positionMap.push(round);
    i++;
  }
  // 存在不存在于二维矩阵中的字符，直接返回false
  if(flag) {
    return false;
  }

  w[0] = positionMap[0][0];
  console.log(positionMap)
};

// 返回所有目标字符的[自身，上，下，左，右]的坐标 
function findRound(board, target) {
  let codes = [];
  // 找出所有节点
  board.forEach((line, i) => {
    line.forEach((code, j) => {
      // 找到节点，查找其上下左右节点
      if(target === code) {
        let up = board[i - 1] ? board[i - 1][j] : '';
        let left = line[j - 1] ? line[j - 1] : '';
        let right = line[j + 1] ? line[j + 1]: '';
        let down = board[i + 1] ? board[i + 1][j] : '';
        codes.push([[i, j], up ? [i - 1, j] : '', down ? [i + 1, j] : '', left ? [i, j - 1] : '', right ? [i, j + 1] : '']);
      }
    })
  })
  return codes;
}

exist([
  ['A','B','C','E'],
  ['S','F','C','S'],
  ['A','D','E','E']
], 'Z')